package es.ats.codington.spring.controllers;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;


/**
 * The Class VisitorController.
 */
@Controller
public class VisitorController {
	
	
	/**
	 * Singup.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_login.htm")
	public String singup(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String n=request.getParameter("username");  
	    String p=request.getParameter("password");
		    
	    Visitor u=new Visitor();
	    u.setUsername(n);
	    u.setPassword(p);
	    VisitorService vs=new VisitorService();
	    String msg = null;
	    
	    try{
		    int idvisitor =vs.loginVisitor(u);
		    if (idvisitor ==0){	
		       	return "mainpage";
		    }else{
		    	HttpSession session = request.getSession(true);
		    	if(idvisitor==1){
		    	  Integer iD = idvisitor;
				    session.setAttribute("iDVisitor", iD);
				    session.setAttribute("username", n);
				    session.setAttribute("password",p);
				    return "mainpage2";
				}else{
		    		Integer iD = idvisitor;
				    session.setAttribute("iDVisitor", iD);
				    session.setAttribute("username", n);
				    session.setAttribute("password",p);
				    return "mainpage2";
		    	}
		    }
	        
		}catch(Exception ex){
			msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			return "mainpage";
		}
	}
	
	/**
	 * Un singup.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_logout.hmt")
	public String unSingup(HttpServletRequest request, HttpServletResponse response) throws IOException {
				
		HttpSession session = request.getSession();
		 session.setAttribute("iDVisitor", null);
		 session.invalidate();
		 return "mainpage";
	}
	
	/**
	 * Preregister.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_register.htm")
	public String preregister(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "register";
	}
	
	/**
	 * Postregister.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/post_register.htm")
	public String postregister(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String name=request.getParameter("name");  
	    String lastname=request.getParameter("lastname");
	    String email=request.getParameter("email");
	    String address=request.getParameter("address");
	    int phone= Integer.parseInt(request.getParameter("phone"));  
	    String dni=request.getParameter("dni");  
	    String username=request.getParameter("username");
	    String password=request.getParameter("password");  
	    		   
	    Visitor u=new Visitor();
	    u.setName(name);
	    u.setLastname(lastname);
	    u.setMail(email);
	    u.setAddress(address);
	    u.setPhone(phone);
	    u.setDni(dni);
	    u.setUsername(username);
	    u.setPassword(password);
	    
	    VisitorService vs=new VisitorService();
	
	    try{
	    	boolean tt =vs.registerVisitor(u);
		    if (tt==true){	
		    	String msg= "You have been registered on our website. Now LogIn to access.";
				request.setAttribute("msg",msg);  	    
				return "mainpage";
		    }else{
		    	String msg= "Sorry, register wrong";
				request.setAttribute("msg",msg);  
				return "mainpage";
		    }
	    }catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			return "mainpage";
	    }
	}
	
	/**
	 * Pre update password.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_updatepassword.htm")
	public String preUpdatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "modifypassword";
	}
	
	/**
	 * Post update password.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/post_updatepassword.htm")
	public String postUpdatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String n=request.getParameter("username");  
	    String op=request.getParameter("oldpassword");  
	    String np=request.getParameter("newpassword");
		    
	    Visitor u=new Visitor();
	    u.setUsername(n);
	    u.setPassword(op);
	    
	    VisitorService vs=new VisitorService();
	   
	    Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
	    
	    try{
		    boolean tt =vs.updatePassword(u,np,id);
		    if (tt==true){	
		    	return "mainpage2";		    	
		    }else{
		    	return "mainpage";	
		    }
		}catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			return "mainpage";
		}
		
	}
	
	/**
	 * Pre update data.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_updatedata.htm")
	public String preUpdateData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "update";
	}
	
	/**
	 * Post update data.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/post_updatedata.htm")
	public String postUpdateData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String name=request.getParameter("name");  
	    String lastname=request.getParameter("lastname");
	    String dni=request.getParameter("dni");  
	    String username=request.getParameter("Username");
	    String password=request.getParameter("password");  
	    String email=request.getParameter("email");
	    int phone= Integer.parseInt(request.getParameter("phone"));  
	    String address=request.getParameter("address");
		    
	    Visitor u=new Visitor();
		    u.setUsername(username);
		    u.setPassword(password);
		    u.setName(name);
		    u.setLastname(lastname);
		    u.setMail(email);
		    u.setDni(dni);
		    u.setAddress(address);
		    u.setPhone(phone);
	    VisitorService vs=new VisitorService();

	    Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
	 
	    try{
	    	boolean tt =vs.updateVisitor(u, id);
	    	if (tt==true){	
			    return "mainpage2";
	    	}else{
	    		return "mainpage";
	    	}
	    
		}catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
				return "mainpage";
		}
		
	}
	
	
	

}
